<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<%@ include file="/extjs/include/all.jsp"%>
<link href="${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl/userModule/page/css/authority.css" rel="stylesheet" type="text/css">
<title>修改机构</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/js/validate.js">
</script>
</head>
<body>
<div id="deptEdit"></div>
</body>
</html>
<script>
var userName = '${__SATURN_AUTORITY_USER__.username}';
var userType = '${__SATURN_AUTORITY_USER__.usertype}';
Ext.onReady(function(){

	Ext.QuickTips.init();  
	
	function getDeptResult(){
		Ext.Ajax.request({
		     method : 'POST',                             //发送方式.'POST'或'GET',一般是'POST'
		     url: '${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl::/departmentModule/action/QueryDeptSaturnDataByID.action',                  //发送到页面
		     success:function(request){                   //发送成功的回调函数
			       	var myResult = Ext.util.JSON.decode(request.responseText);
			       	Ext.get("code").dom.value = myResult.deptSaturnData.code;
			       	Ext.get("id").dom.value = myResult.deptSaturnData.id;
			       	Ext.get("id_show").dom.value = myResult.deptSaturnData.id;
			       	Ext.get("name").dom.value = myResult.deptSaturnData.name;
			       	Ext.get("descn").dom.value = myResult.deptSaturnData.descn;
			       	Ext.get("parent_id").dom.value = myResult.deptSaturnData.parent_id; 
			       	Ext.get("parent_id_show").dom.value = myResult.deptSaturnData.parent_id; 
			       	Ext.get("parent_name").dom.value = myResult.deptSaturnData.parent_name;
		     },
		     failure:function(){                          //发送失败的回调函数
		       		Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
		     },
		     params:{
		     		deptId:'<%=request.getParameter("deptId")%>'
		     } 
     	});
    }
    getDeptResult();
	var deptForm = new Ext.FormPanel({
		labelWidth: 75,
	    id:'deptForm',
	    frame:true,
	    bodyStyle:'padding:5px 5px 0',
	    width: 350,
	    defaults: {width: 230},
	    defaultType: 'textfield',
	    items: [ {
	            name: 'id',
	            id: 'id',
	            inputType:'hidden',
	            hidden:'true'
	        },{
	            name: 'parent_id',
	            id: 'parent_id',
	            inputType:'hidden',
	            hidden:'true'
	        },{
	        	fieldLabel: '机构全码',
	            name: 'code',
	            id: 'code',
	            disabled:true
	        },{
	            fieldLabel: '机构ID',
	            name: 'id_show',
	            id: 'id_show',
	            disabled:true
	        },{
	            fieldLabel: '机构名称',
	            name: 'name',
	            id: 'name',
	            maxLength:20,
	            vtype: 'isNumberAlphaUnderlineChinese'
	        }, {
	        	xtype:'textarea',
	        	fieldLabel: '描述',
	            name: 'descn',
	            id: 'descn',
	            //maxLength:20,
	            vtype: 'isNumberAlphaUnderlineChineseSpaceMaxLength200'
	        },{
	            fieldLabel: '父机构',
	            name: 'parent_id_show',
	            id: 'parent_id_show',
	            disabled:true
	        },{
	            fieldLabel: '父机构名称',
	            name: 'parent_name',
	            id: 'parent_name',
	            disabled:true
	        }
	    ],

        buttons: [{
            text: '删除',
            handler: delDept
        },{
            text: '添加子机构',
            handler: addSubDept
        },{
            text: '修改',
            handler: updateDept
        }]
	});
	deptForm.render('deptEdit');
	function  getStr(Str){//去掉字符串中的空格
	    var source = Str ;
	    var len = source.length;
	    var s = "";
	    for(i=0,j=0;i<len;i++){
			if(source.charAt(i)==' ') continue;
			else{
				s+=source.charAt(i);
			}
	    }
	    return s;
    }
	function delDept(){
	     var code = Ext.get("code").dom.value;
	     if(getStr(code)=="000009"){
	            Ext.Msg.alert("信息", "总部机构不能删除！");
				return;
	     }
	     Ext.MessageBox.confirm('警告','确实要删除吗？', 
	     function(btn){
		     if(btn == 'yes'){
			     deptForm.form.submit({
					     method : 'POST',                             
					     url : '${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl::/departmentModule/action/DeleteDept.action',              
					     success : function(form, action) {
			                	var myResult = Ext.util.JSON.decode(action.response.responseText);	
			                	Ext.Msg.alert('信息',myResult.message, function(){
			                		window.parent.refreshTree(Ext.get('parent_id').dom.value);
			                	});	
			             },
			             failure : function(form, action) {
			                    Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
			             },
			             waitMsg:'正在删除机构，请稍后...'
			     	});			     	
			}
		});
	}
	
	function addSubDept(){
   		var form = deptForm.getForm().getEl().dom;   
        form.action = '${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl/departmentModule/page/deptInsert.jsp';   
        form.target = '_self';   
        form.submit();
	}
	function updateDept(){
	    var name = Ext.get("name").dom.value;
	    if(getStr(name)==""){
				Ext.Msg.alert("信息", "请输入机构名称！");
				return;
		}
	    deptForm.form.submit({
		     method : 'POST',                             
		     url : '${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl::/departmentModule/action/UpdateDept.action',              
		     success : function(form, action) {
                	var myResult = Ext.util.JSON.decode(action.response.responseText);	
                	Ext.Msg.alert('信息',myResult.message, function(){
                		window.parent.refreshTree(Ext.get('id').dom.value);
                	});	
             },
             failure : function(form, action) {
                    Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
             },
             waitMsg:'正在修改机构，请稍后...'
		});
	}
	
	document.getElementById('name').maxLength = 20;
});

</script>